﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace Maintain_supplier_information
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SupplierBizlogic sbz = new SupplierBizlogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //DataSet1TableAdapters.s1TableAdapter ta = new DataSet1TableAdapters.s1TableAdapter();
                //DataSet1.s1DataTable dt = new DataSet1.s1DataTable();
                //ta.Fill(dt);
                //GridView1.DataSource = dt;
                //GridView1.DataBind();
                if (ddl_Category.SelectedItem.Text == "All")
                {

                    GridView1.DataSource = sbz.ret_sta_items(ddl_Category.SelectedValue);
                    GridView1.DataBind();
                }

            }


        }


        protected void btn_search_Click(object sender, EventArgs e)
        {
            List<object> list = new List<object>();
            StationeryInventoryEntities1 se = new StationeryInventoryEntities1();
            string category;
            category = ddl_Category.SelectedValue;


            if (ddl_Category.SelectedItem.Text == "All")
            {
                list = sbz.ret_sta_items_all();
            }
            else
            {

                list = sbz.ret_sta_items(ddl_Category.SelectedValue);
            }

            GridView1.DataSource = list;
            GridView1.DataBind();
            gdview_details.DataSource = null;
            gdview_details.DataBind();

        }



        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;

            btn_search_Click(sender, e);

        }

        //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int te = e.RowIndex;
        //    string ItemNumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
        //    StationeryInventoryEntities1 st = new StationeryInventoryEntities1();
        //    Stationery m = st.Stationeries.FirstOrDefault(x => x.ItemCode == ItemNumber);

        //    SupplierDetail s = st.SupplierDetails.FirstOrDefault(y => y.ItemCode == ItemNumber);
        //    st.SupplierDetails.Remove(s);

        //    st.Stationeries.Remove(m);
        //    st.SaveChanges();
        //    btn_search_Click(sender, e);
        //    DataBind();

        //}

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int te = e.RowIndex;
            string ItemNumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
            StationeryInventoryEntities1 st = new StationeryInventoryEntities1();
            var m = (from u in st.Stationeries
                     where u.ItemCode == ItemNumber
                     select u).Single();

            TextBox reorder_level = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
            TextBox reorder_qty = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
            m.ReorderLevel = Convert.ToInt32(reorder_level.Text);
            m.ReorderQty = Convert.ToInt32(reorder_qty.Text);
            st.SaveChanges();
            btn_search_Click(sender, e);
            GridView1.EditIndex = -1;
            DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            btn_search_Click(sender, e);
        }



        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int te = Convert.ToInt32(e.CommandArgument);
                List<object> list = new List<object>();
                string itemnumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
                //GridView1.Rows[te].Cells[0].Text;
                StationeryInventoryEntities1 se = new StationeryInventoryEntities1();
                var q = from st in se.SupplierDetails
                        join c in se.Suppliers on st.SupplierID equals c.SupplierID
                        where st.ItemCode == itemnumber
                        select new { st.ItemCode, st.Rank, c.SupplierName, c.PhoneNo, st.Price };
                list = q.ToList<object>();
                gdview_details.DataSource = list;
                gdview_details.DataBind();

            }


        }

        protected void Btn_AddNewItem_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AddItem.aspx");
        }

    }
}